package com.hjq.demo.yuliao.dialog

import android.content.*
import android.view.*
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import com.hjq.base.BaseDialog
import com.hjq.base.action.AnimAction
import com.hjq.demo.R
import com.hjq.demo.aop.SingleClick

/**
 *
 * @author jiahe
 * 创建日期 2022/2/15
 * 描述 麦位弹框
 *
 */
class MaiWeiDialog {

    @Suppress("UNCHECKED_CAST", "LeakingThis")
    open class Builder<B : Builder<B>>(context: Context) : BaseDialog.Builder<B>(context) {

        private var autoDismiss = true

        private val containerLayout: ViewGroup? by lazy { findViewById(R.id.ll_ui_container) }
        private val titleView: TextView? by lazy { findViewById(R.id.tv_maihui_title) }
        private val desView: TextView? by lazy { findViewById(R.id.tv_maihui_des) }
        private val levelView: TextView? by lazy { findViewById(R.id.iv_maiwei_level) }
        private val cancelView: TextView? by lazy { findViewById(R.id.tv_ui_cancel) }
        private val lineView: View? by lazy { findViewById(R.id.v_ui_line) }
        private val confirmView: TextView? by lazy { findViewById(R.id.tv_ui_confirm) }

        init {
            setContentView(R.layout.ui_dialog_maiwei)
            setAnimStyle(AnimAction.ANIM_IOS)
            setGravity(Gravity.CENTER)
            setOnClickListener(cancelView, confirmView)

            setOnClickListener(R.id.tv_maihui_attention,R.id.tv_maiwei_sixin,R.id.tv_maiwei_gift)
        }

        fun setCustomView(@LayoutRes id: Int): B {
            return setCustomView(LayoutInflater.from(getContext()).inflate(id, containerLayout, false))
        }

        fun setCustomView(view: View?): B {
            containerLayout?.addView(view, 1)
            return this as B
        }

        fun setTitle(@StringRes id: Int): B {
            return setTitle(getString(id))
        }

        fun setTitle(text: CharSequence?): B {
            titleView?.text = text
            return this as B
        }

        fun setDes(text: CharSequence?): B {
            desView?.text = text
            return this as B
        }

        fun setCancel(@StringRes id: Int): B {
            return setCancel(getString(id))
        }

        fun setCancel(text: CharSequence?): B {
            cancelView?.text = text
            lineView?.visibility = if (text == null || "" == text.toString()) View.GONE else View.VISIBLE
            return this as B
        }

        fun setConfirm(@StringRes id: Int): B {
            return setConfirm(getString(id))
        }

        fun setConfirm(text: CharSequence?): B {
            confirmView?.text = text
            return this as B
        }

        fun setAutoDismiss(dismiss: Boolean): B {
            autoDismiss = dismiss
            return this as B
        }

        fun autoDismiss() {
            if (autoDismiss) {
                dismiss()
            }
        }

        @SingleClick
        override fun onClick(view: View) {
            when (view.id) {
                R.id.tv_maihui_attention -> {
                    autoDismiss()
                    listener?.onAttention(getDialog())
                }
                R.id.tv_maiwei_sixin -> {
                    autoDismiss()
                    listener?.onSiXiin(getDialog())
                }
                R.id.tv_maiwei_gift -> {
                    autoDismiss()
                    listener?.onGift(getDialog())
                }
            }
        }

        private var listener: OnListener? = null

        fun setListener(listener: OnListener?) = apply {
            this.listener = listener
        }

        interface OnListener {

            /**
             * 点击关注时回调
             */
            fun onAttention(dialog: BaseDialog?)

            /**
             * 点击私信时回调
             */
            fun onSiXiin(dialog: BaseDialog?) {}

            /**
             * 点击礼物时回调
             */
            fun onGift(dialog: BaseDialog?) {}
        }
    }
}